const fs = require('fs');
const path = require('path');
const chalk = require('chalk'); // 为控制台输出的文字着色
const symbols = require('log-symbols'); // 终端图标显示
const inquirer = require('inquirer');
const prompsConfig = require('./config/promps');
const log = require('./config/log');
const creatFiles = require('./creat-file');
const vueTemplate = require('./config/template/vue-template')
const lessTemplate = require('./config/template/less-template')

let rootPath = '../src';
init();

async function init() {
    let args = process.argv.splice(2) || [];
    let name = args[0];
    if (!args || !args.length) log('error', '参数错误', true);
    if (!name) log('error', '创建文件夹名称不可空', true);

    let { type } = await inquirer.prompt(prompsConfig.creatType);
    mkdir(rootPath, type, name).then(async (res)=>{
        log('success', res.msg);
        let { state } = await inquirer.prompt(prompsConfig.creatTemplate);
        let creatPath = path.join(__dirname, `${rootPath}/${type}/${name}`);
        if(state){
            creatFiles([
                { 
                    path: creatPath, 
                    name: 'index.vue',
                    content: vueTemplate.template
                },
                {
                    path: creatPath, 
                    name: 'index.less',
                    content: lessTemplate.template
                }
            ]);
        }
    }).catch(err=>{
        log('error', err.msg, true);
    });
}



function mkdir(rootPath, type, name){
    let creatPath = path.join(__dirname, `${rootPath}/${type}/${name}`);
    return new Promise((resolve, reject)=>{
        if(!fs.existsSync(creatPath)){
            fs.mkdirSync(creatPath);
            resolve({state: -1, msg: `${type}中文件夹${name}创建成功`})
        } else {
            reject({state: -1, msg: `${type}中文件夹${name}已存在`})
        }
    })
}